import 'package:flutter/material.dart';
import './widgets/Game2048Score.dart';
import './widgets/Game2048ChessBoard.dart';
import './service/GameUtil.dart';
// import 'package:shared_preferences/shared_preferences.dart';

/// 游戏2048
/// 
/// https://github.com/zh8637688/Flutter-Example
/// 
class Game2048Home extends StatefulWidget {
  @override
  _Game2048HomeState createState() => new _Game2048HomeState();
}

class _Game2048HomeState extends State<Game2048Home> {
  int score;
  int best;
  List<int> chess;

  @override
  void initState() {
    super.initState();
    setState(() {
      score = 0;
      best = 0;
      chess = initChess();
    });
    _getBestFromLocal();
  }

  _getBestFromLocal() async {
    // SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      // best = prefs.getInt('best') ?? 0;
    });
  }

  _saveBestToLocal(int best) async {
    // SharedPreferences prefs = await SharedPreferences.getInstance();
    // prefs.setInt('best', best);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Flutter 2048'),
        backgroundColor: new Color(0xff3c3c3a),
      ),
      body: new Container(
        color: new Color(0xfffaf8ef),
        child: new GestureDetector(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Game2048Score(score: score, best: best),
              new Game2048ChessBoard(chess: chess)
            ],
          ),
          onHorizontalDragEnd: onHorizontalDragEnd,
          onVerticalDragEnd: onVerticalDragEnd,
        ),
      ),
    );
  }

  onHorizontalDragEnd(details) {
    var result;
    if (details.primaryVelocity > 0.0) {
      result = flingTo(this.chess, 0);
    } else {
      result = flingTo(this.chess, 1);
    }
    int score = this.score + result['score'];
    if (score > best) {
      best = score;
      _saveBestToLocal(best);
    }
    setState(() {
      this.score = score;
      this.best = best;
      this.chess = result['chess'];
    });
  }

  onVerticalDragEnd(details) {
    var result;
    if (details.primaryVelocity < 0.0) {
      result = flingTo(this.chess, 2);
    } else {
      result = flingTo(this.chess, 3);
    }
    int score = this.score + result['score'];
    if (score > best) {
      best = score;
      _saveBestToLocal(best);
    }
    setState(() {
      this.score = score;
      this.best = best;
      this.chess = result['chess'];
    });
  }
}
